package com.jia.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.mybatis.page.PageResult;
import com.jia.product.entity.Attr;
import com.jia.product.entity.AttrAttrGroupRelation;
import com.jia.product.entity.AttrGroup;
import com.jia.product.entity.Category;
import com.jia.product.mapper.AttrAttrGroupRelationMapper;
import com.jia.product.mapper.AttrGroupMapper;
import com.jia.product.mapper.AttrMapper;
import com.jia.product.mapper.CategoryMapper;
import com.jia.product.service.AttrService;
import com.jia.product.service.CategoryService;
import com.jia.product.vo.AttrRespVo;
import com.jia.product.vo.AttrVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品属性(Attr)表服务实现类
 *
 * @author makejava
 * @since 2025-07-08 18:15:25
 */
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrMapper, Attr> implements AttrService {

    @Autowired
    private AttrAttrGroupRelationMapper attrAttrGroupRelationMapper;

    @Autowired
    private AttrGroupMapper attrGroupMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrMapper attrMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveVo(AttrVo attrVo) {
        Attr attr = new Attr();
        BeanUtils.copyProperties(attrVo, attr);
        this.save(attr);
        AttrAttrGroupRelation attrAttrGroupRelation = new AttrAttrGroupRelation();
        attrAttrGroupRelation.setAttrGroupId(attrVo.getAttrGroupId());
        attrAttrGroupRelation.setAttrId(attr.getAttrId());
        attrAttrGroupRelationMapper.insert(attrAttrGroupRelation);
    }

    @Override
    public PageResult<AttrRespVo> pageQuery(long current, long size, String key, String attrType, Long catelogId) {
        Page<AttrRespVo> result = new Page<>(current, size);

        Page<Attr> page = new Page<>(current, size);

        QueryWrapper<Attr> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((obj) -> {
                obj.like("attr_name", key);
            });
        }
        Page<Attr> attrResult;
        queryWrapper.eq("attr_type", "base".equalsIgnoreCase(attrType) ? 1 : 0);
        if (catelogId != 0) {
            queryWrapper.eq(true, "catelog_id", catelogId);
        }
        attrResult = this.page(page, queryWrapper);

        List<Attr> records = attrResult.getRecords();

        // 低配写法
        List<AttrRespVo> collect = records.stream().map((obj) -> {
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(obj, attrRespVo);
            Long attrId = obj.getAttrId();
            AttrAttrGroupRelation relation = attrAttrGroupRelationMapper
                    .selectOne(new QueryWrapper<AttrAttrGroupRelation>().eq("attr_id", attrId));
            if (relation != null) {
                Long attrGroupId = relation.getAttrGroupId();
                AttrGroup attrGroup = attrGroupMapper.selectById(attrGroupId);
                attrRespVo.setAttrGroupId(attrGroupId);
                attrRespVo.setAttrGroupName(attrGroup.getAttrGroupName());
            }
            Category category = categoryMapper.selectById(obj.getCatelogId());
            attrRespVo.setCategoryName(category.getName());
            return attrRespVo;
        }).collect(Collectors.toList());


        result.setRecords(collect);
        result.setTotal(attrResult.getTotal());
        result.setCurrent(attrResult.getCurrent());
        result.setSize(attrResult.getSize());
        result.setPages(attrResult.getPages());
        return PageResult.convert(result);
    }

    @Override
    public AttrRespVo selectInfo(Long attrId) {
        AttrRespVo attrRespVo = new AttrRespVo();
        Attr attr = this.getBaseMapper().selectById(attrId);
        BeanUtils.copyProperties(attr, attrRespVo);

        // 分组信息
        if (attr.getAttrType().equals(1)) {
            AttrAttrGroupRelation relation = attrAttrGroupRelationMapper
                    .selectOne(new QueryWrapper<AttrAttrGroupRelation>().eq("attr_id", attrId));
            if (relation != null) {
                Long attrGroupId = relation.getAttrGroupId();
                AttrGroup attrGroup = attrGroupMapper.selectById(attrGroupId);
                attrRespVo.setAttrGroupId(attrGroupId);
                attrRespVo.setAttrGroupName(attrGroup.getAttrGroupName());
            }
        }

        // 分类信息
        Long catelogId = attr.getCatelogId();
        Category category = categoryMapper.selectById(catelogId);
        if (category != null) {
            attrRespVo.setCategoryName(category.getName());
            Long[] catelogPath = categoryService.findCatelogPath(catelogId);
            attrRespVo.setCatelogPath(catelogPath);
        }

        return attrRespVo;
    }

    @Override
    public AttrVo updateInfo(AttrVo attrVo) {
        Attr attr = new Attr();
        BeanUtils.copyProperties(attrVo, attr);
        attrMapper.updateById(attr);

        if (attrVo.getAttrType().equals(0)) {
            return attrVo;
        }

        Long attrGroupId = attrVo.getAttrGroupId();

        if (attrGroupId != null) {
            AttrAttrGroupRelation relation = attrAttrGroupRelationMapper
                    .selectOne(new QueryWrapper<AttrAttrGroupRelation>().eq("attr_id", attr.getAttrId()));
            if (relation == null) {
                relation = new AttrAttrGroupRelation();
                relation.setAttrId(attr.getAttrId());
                relation.setAttrGroupId(attrGroupId);
                attrAttrGroupRelationMapper.insert(relation);
            } else {
                if (!relation.getAttrGroupId().equals(attrGroupId)) {
                    relation.setAttrGroupId(attrGroupId);
                    attrAttrGroupRelationMapper.updateById(relation);
                }
            }
        }


        return attrVo;
    }

}

